﻿'
' Bring2mind - http://www.bring2mind.net
' Copyright (c) 2011
' by Bring2mind
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports Bring2mind.DNN.Modules.Signup.Business
Imports DotNetNuke.Services.Localization.Localization

Public Class Signup
 Inherits ModuleBase

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  If Not Me.IsPostBack Then

   If UserId <> -1 Then
    ' user is logged in
    If Settings.NewsletterRoleId > -1 Then
     pnlSignedUp.Visible = True
     If (New DotNetNuke.Security.Roles.RoleController).GetUserRole(PortalId, UserId, Settings.NewsletterRoleId) Is Nothing Then
      lblSubscribeStatus.Text = GetString("Unsubscribed.Status", LocalResourceFile)
      cmdSubscribe.Text = GetString("cmdSubscribe.Text", LocalResourceFile)
     Else
      lblSubscribeStatus.Text = GetString("Subscribed.Status", LocalResourceFile)
      cmdSubscribe.Text = GetString("cmdUnsubscribe.Text", LocalResourceFile)
     End If
    End If
   Else
    pnlSignup.Visible = True
   End If

  End If

 End Sub

 Private Sub cmdSignup_Click(sender As Object, e As System.EventArgs) Handles cmdSignup.Click

  Dim signup As New SignupInfo
  With signup
   .DateAdded = Now
   .Email = txtEmail.Text.Trim
   .MagicKey = Guid.NewGuid().ToString
   .PortalId = PortalId
  End With
  signup.SignupID = SignupsController.AddSignup(signup)
  Dim confirmUrl As String = EditUrl("key", signup.MagicKey, "confirm")
  Dim emailSubject As String = GetString("EMAIL_CONFIRM_SUBJECT", Common.glbSharedResourcesFile)
  Dim emailBody As String = GetString("EMAIL_CONFIRM_BODY", Common.glbSharedResourcesFile)
  If String.IsNullOrEmpty(emailBody) Then
   emailBody = "Please click here to register: [confirmurl]"
  End If
  If String.IsNullOrEmpty(emailSubject) Then
   emailSubject = "Registration confirmation"
  End If
  emailBody = Replace(emailBody, "[confirmurl]", confirmUrl, 1, -1, CompareMethod.Text)
  Dim tr As New DotNetNuke.Services.Tokens.TokenReplace
  emailSubject = tr.ReplaceEnvironmentTokens(emailSubject)
  emailBody = tr.ReplaceEnvironmentTokens(emailBody)
  Dim result As String = DotNetNuke.Services.Mail.Mail.SendMail(PortalSettings.Email, signup.Email, "", emailSubject, emailBody, "", "Html", "", "", "", "")
  If result = "" Then
   pnlSignup.Visible = False
   pnlSigningUp.Visible = True
  End If

  If Settings.RedirectAfterSignupTabId > 0 Then
   Response.Redirect(DotNetNuke.Common.NavigateURL(Settings.RedirectAfterSignupTabId), False)
  End If

 End Sub

 Private Sub cmdSubscribe_Click(sender As Object, e As System.EventArgs) Handles cmdSubscribe.Click

  If Settings.NewsletterRoleId > -1 Then
   Dim objRoles As New DotNetNuke.Security.Roles.RoleController
   Dim urc As New DotNetNuke.Security.Roles.RoleController
   If urc.GetUserRole(PortalId, UserId, Settings.NewsletterRoleId) Is Nothing Then
    urc.AddUserRole(PortalId, UserId, Settings.NewsletterRoleId, DotNetNuke.Common.Utilities.Null.NullDate)
    lblSubscribeStatus.Text = GetString("Subscribed.Status", LocalResourceFile)
    cmdSubscribe.Text = GetString("cmdUnsubscribe.Text", LocalResourceFile)
   Else
    urc.DeleteUserRole(PortalId, UserId, Settings.NewsletterRoleId)
    If urc.GetUserRole(PortalId, UserId, Settings.NewsletterRoleId) Is Nothing Then
     lblSubscribeStatus.Text = GetString("Unsubscribed.Status", LocalResourceFile)
     cmdSubscribe.Text = GetString("cmdSubscribe.Text", LocalResourceFile)
    End If
   End If
  End If

 End Sub

End Class